package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import bean.Descriptionbean;
import bean.Masterbean;
import bean.Petbean;
import bean.Userbean;
import conn.ConnectDatabase;
import service.IDesDao;
import service.IMasterDao;
import service.IUserDao;

public class DesDaoImp implements IDesDao {
	 PreparedStatement ps = null;
	 Date date = new Date();
		//得到一个timestamp格式的时间，存入mysql中的时间格式为"yyyy-MM-dd HH:mm:ss"
		Timestamp timestamp = new Timestamp(date.getTime());
	 
	public boolean insert(Descriptionbean p) {
		// TODO 自动生成的方法存根
		String sql ="insert into description (pet_id,doctor,sickness,data) values(?,?,?,?)";
		String sql1= "insert into history(description,data) values(?,?)";
		int n =0;
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		
		ps.setInt(1, p.getPet_id());
		ps.setString(2, p.getDoctor());
		ps.setString(3, p.getSickness());
		ps.setString(4, p.getData());
		n=ps.executeUpdate();
		
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "增加了一次病例信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		
		}catch (Exception e) {
		e.printStackTrace();
		}
		return n>0;
	}

	public boolean delete(Descriptionbean p) {
		// TODO 自动生成的方法存根
		String sql ="delete from description where pet_id="+p.getPet_id();
		String sql1= "insert into history(description,data) values(?,?)";
		int n=0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		n=ps.executeUpdate();
		
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "删除了一次病例信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return n>0;
	}

	@Override
	public boolean update(Descriptionbean p) {
		return false;
	
	}

	public List<Descriptionbean> queryDes(Map<String, Object> map) {
		StringBuffer sql= new StringBuffer("select * from description where 1 =1");

		List<Descriptionbean> list = new ArrayList<Descriptionbean>();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			Descriptionbean p = new Descriptionbean();
			p.setPet_id(rs.getInt("pet_id"));
			p.setDoctor(rs.getString("doctor"));
			p.setSickness(rs.getString("sickness"));
			p.setData(rs.getString("data"));
			list.add(p);
			
			
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	
	
	@Override
	public Descriptionbean queryById(Descriptionbean p) {
		Descriptionbean  pet = null;
		String sql = "select * from description where id="+p.getPet_id();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			pet = new Descriptionbean();
			pet.setPet_id(rs.getInt("pet_id"));
			pet.setDoctor(rs.getString("doctor"));
			pet.setSickness(rs.getString("sickness"));
			pet.setData(rs.getString("data"));
					
					
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pet;
	}

	

	



}
